<template>
  <div class="list">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-input placeholder="请输入内容" v-model="searchVal" class="search-input">
      <el-button slot="append" icon="el-icon-search"  @click='initList'></el-button>
    </el-input>
    <el-button type="primary" plain @click='jumpToAdd'>添加商品</el-button>
  <!-- 表格 -->
   <el-table
    class="mt-15 mb-15"
    :data="productList"
    border
    stripe
    style="width: 100%">
    <el-table-column
      type="index"
      label="编号"
      width="tabWidths[0]">
    </el-table-column>
    <el-table-column
      prop="goods_name"
      label="商品名称"
      width="tabWidths[1]">
    </el-table-column>
    <el-table-column
      prop="goods_price"
      label="商品价格"
      width="tabWidths[2]">
    </el-table-column>
    <el-table-column
      prop="goods_state"
      label="商品状态"
      width="tabWidths[3]">
    </el-table-column>
    <el-table-column
      prop="goods_weight"
      label="商品重量"
      width="tabWidths[4]">
    </el-table-column>
    <el-table-column
      width="tabWidths[5]"
      label="创建时间">
      <template slot-scope="scope">
        {{scope.row.add_time |  fmtTime}}
      </template>
    </el-table-column>
    <el-table-column
      label="操作">
      <template slot-scope="scope">
        <el-button size="mini" icon="el-icon-edit" plaintype="primary" @click='edit(scope.row)'></el-button>
        <el-button size="mini" icon="el-icon-delete" plain type="danger" @click='del(scope.row)'></el-button>
        <el-button size="mini" icon="el-icon-check" plain type="warning" @click='grantUser(scope.row)'></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum">
      </el-pagination>
    </div>
    <!-- 展示添加商品框 -->
  </div>
</template>
<script>
  import { getProductList ,delProductList} from "@/api";
  export default {
    data() {
      return {
        // 分页的数据 当前页 总条数 每页条数
        currentPage: 1,
        totalNum: 0,
        pageSize: 10,
        // 搜索
        searchVal: "",
        tabWidths: ['50px', '350px', '80px', '80px', '100px', '130px', '260px'],
        productList: [
          {
            goods_id: 144,
            goods_name: "asfdsd",
            goods_price: 1,
            goods_number: 1,
            goods_weight: 1,
            goods_state: null,
            add_time: 1512954923,
            upd_time: 1512954923,
            hot_mumber: 0,
            is_promote: false
          }
        ]
      };
    },
    created() {
      this.initList();
    },
    filters: {
      fmtTime(addTime){
        addTime= new Date(addTime)
        var y=addTime.getFullYear()
        var m=addTime.getMonth()+1
        var d=addTime.getDate()
        return y+'/'+m+'/'+d
      }
    },
    methods: {
      // 跳转添加页面
      jumpToAdd(){
        this.$router.push({name:"AddGoods"})
      },
      jumpToEdit(){
        this.$router.push({name:'EditGoods'})
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.initList();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.initList();
      },
      // 初始化商品列表
      initList() {
        getProductList({
          query: this.searchVal,
          pagenum: this.currentPage,
          pagesize: this.pageSize
        }).then(res => {
          console.log(res);
          if (res.meta.status === 200) {
            this.totalNum=res.data.total;
            this.productList = res.data.goods;
          }
        });
      },
      // 删除商品
      del(row){
        // delProductList
      }
    }
  };
</script>
<style lang="scss" scoped>
  .list {
    .search-input {
      width: 300px;
    }
  }
  .pname {
    background-color: #ecf5ff;
    height: 30px;
    line-height: 30px;
    padding-left: 10px;
    border: 1px solid #ecf5ff;
  }
  .pvalue {
    height: 30px;
    line-height: 30px;
    border: 1px solid #ecf5ff;
    padding-left: 10px;
  }
  .el-tag {
    margin-left: 5px;
  }
  .input-new-tag {
    width: 100px;
  }
</style>